import * as NS from "../../../Common/NetServices";
import { IsNull } from "../../../TYLibrary/Common/GlobalExtend";
import { Wx_XSPPage } from "../../../Common/Entitys";
import { Log } from "../../../TYLibrary/System/Logger";
import { Wx_PageData } from "../../../Common/EntityBase";


class TextPageData extends Wx_PageData {
    public optionsListData = [];
    public movableViewPosition: any = {
        x: 0,
        y: 0,
        className: "none",
        data: {}
    };
    public scrollPosition: any = {
        everyOptionCell: 65,
        left: 20,
        top: 47,
        scrollTop: 0,
        scrollY: true,
        scrollViewHeight: 1000,
        scrollViewWidth: 375,
        windowViewHeight: 1000,
    };
    public selectItemInfo: any = {
        sName: "",
        sDtSecCode: "",
        sCode: "",
        selectIndex: -1,
        selectPosition: 0,
    };
    public lastX: number = 0;
    public lastY: number = 0;
    public text: string = "没有滑动";
    public currentGesture: number = 0;
    public ballTop: number = 0;
    public ballLeft: number = 0;
}


class IndexPage extends Wx_XSPPage {
    public Data: TextPageData = new TextPageData();

    /**
     * 初始化页面信息
     * @param {any} options
     * @returns {Promise<void>}
     * @memberof IndexPage
     */
    public async InitData(options: any): Promise<void> {
        // var systemInfo = wx.getSystemInfoSync();
        // var optionsList = [
        //     { "sCode": "600879", "sDtSecCode": "0101600879", "sName": "新品 甜曲紫米面包1100g 买一赠一实...", "iUpdateTime": 1496362315, "fNow": "--", "sRate": "--", "className": "block_wave block_stop", "srcimg": "https://www.e-taiyun.cn/XCXFiles/xsp/img/pic/pic_16.jpg" },
        //     { "sCode": "00700", "sDtSecCode": "020100700", "sName": "新品 甜曲紫米面包1100g 买一赠一实...", "iUpdateTime": 1495531418, "fNow": "--", "sRate": "--", "className": "block_wave block_stop", "srcimg": "https://www.e-taiyun.cn/XCXFiles/xsp/img/pic/pic_16.jpg" },
        //     { "sCode": "000759", "sDtSecCode": "0001000759", "sName": "认养一头牛 原味酸奶 200gx12盒x2提装", "iUpdateTime": 1495531394, "fNow": "--", "sRate": "--", "className": "block_wave block_stop", "srcimg": "https://www.e-taiyun.cn/XCXFiles/xsp/img/pic/pic_18.jpg" },
        //     { "sCode": "000651", "sDtSecCode": "0001000651", "sName": "新品 甜曲紫米面包1100g 买一赠一实...", "iUpdateTime": 1495531384, "fNow": "--", "sRate": "--", "className": "block_wave block_stop", "srcimg": "https://www.e-taiyun.cn/XCXFiles/xsp/img/pic/pic_17.jpg" },
        //     { "sCode": "600036", "sDtSecCode": "0101600036", "sName": "新品 甜曲紫米面包1100g 买一赠一实...", "iUpdateTime": 1495153104, "fNow": "--", "sRate": "--", "className": "block_wave block_stop", "srcimg": "https://www.e-taiyun.cn/XCXFiles/xsp/img/pic/pic_19.jpg" },
        //     { "sCode": "600518", "sDtSecCode": "0101600518", "sName": "认养一头牛 原味酸奶 200gx12盒x2提装", "iUpdateTime": 1495153089, "fNow": "--", "sRate": "--", "className": "block_wave block_stop", "srcimg": "https://www.e-taiyun.cn/XCXFiles/xsp/img/pic/pic_15.jpg" },
        //     { "sCode": "601238", "sDtSecCode": "0101601238", "sName": "认养一头牛 原味酸奶 200gx12盒x2提装", "iUpdateTime": 1495153077, "fNow": "--", "sRate": "--", "className": "block_wave block_stop", "srcimg": "https://www.e-taiyun.cn/XCXFiles/xsp/img/pic/pic_14.jpg" },
        //     { "sCode": "601989", "sDtSecCode": "0101601989", "sName": "认养一头牛 原味酸奶 200gx12盒x2提装", "iUpdateTime": 1495153069, "fNow": "--", "sRate": "--", "className": "block_wave block_stop", "srcimg": "https://www.e-taiyun.cn/XCXFiles/xsp/img/pic/pic_13.jpg" },
        //     { "sCode": "600519", "sDtSecCode": "0101600519", "sName": "认养一头牛 原味酸奶 200gx12盒x2提装", "iUpdateTime": 1495153058, "fNow": "--", "sRate": "--", "className": "block_wave block_stop", "srcimg": "https://www.e-taiyun.cn/XCXFiles/xsp/img/pic/pic_16.jpg" },
        //     { "sCode": "002230", "sDtSecCode": "0001002230", "sName": "认养一头牛 原味酸奶 200gx12盒x2提装", "iUpdateTime": 1495152519, "fNow": "--", "sRate": "--", "className": "block_wave block_stop", "srcimg": "https://www.e-taiyun.cn/XCXFiles/xsp/img/pic/pic_16.jpg" },
        //     { "sCode": "002431", "sDtSecCode": "0001002431", "sName": "新品 甜曲紫米面包1100g 买一赠一实...", "iUpdateTime": 1494776256, "fNow": "--", "sRate": "--", "className": "block_wave block_stop", "srcimg": "https://www.e-taiyun.cn/XCXFiles/xsp/img/pic/pic_16.jpg" },
        //     { "sCode": "01357", "sDtSecCode": "020101357", "sName": "新品 甜曲紫米面包1100g 买一赠一实...", "iUpdateTime": 1494480437, "fNow": "--", "sRate": "--", "className": "block_wave block_stop", "srcimg": "https://www.e-taiyun.cn/XCXFiles/xsp/img/pic/pic_16.jpg" },
        //     { "sCode": "300222", "sDtSecCode": "0001300222", "sName": "新品 甜曲紫米面包1100g 买一赠一实...", "iUpdateTime": 1493777483, "fNow": "--", "sRate": "--", "className": "block_wave block_stop", "srcimg": "https://www.e-taiyun.cn/XCXFiles/xsp/img/pic/pic_16.jpg" },
        //     { "sCode": "300131", "sDtSecCode": "0001300131", "sName": "新品 甜曲紫米面包1100g 买一赠一实...", "iUpdateTime": 1493777479, "fNow": "--", "sRate": "--", "className": "block_wave block_stop", "srcimg": "https://www.e-taiyun.cn/XCXFiles/xsp/img/pic/pic_16.jpg" },
        //     { "sCode": "001696", "sDtSecCode": "0001001696", "sName": "新品 甜曲紫米面包1100g 买一赠一实...", "iUpdateTime": 1493777476, "fNow": "--", "sRate": "--", "className": "block_wave block_stop", "srcimg": "https://www.e-taiyun.cn/XCXFiles/xsp/img/pic/pic_16.jpg" },
        //     { "sCode": "600503", "sDtSecCode": "0101600503", "sName": "新品 甜曲紫米面包1100g 买一赠一实...", "iUpdateTime": 1493777473, "fNow": "--", "sRate": "--", "className": "block_wave block_stop", "srcimg": "https://www.e-taiyun.cn/XCXFiles/xsp/img/pic/pic_16.jpg" },
        //     { "sCode": "300154", "sDtSecCode": "0001300154", "sName": "认养一头牛 原味酸奶 200gx12盒x2提装", "iUpdateTime": 1493177346, "fNow": "--", "sRate": "--", "className": "block_wave block_stop", "srcimg": "https://www.e-taiyun.cn/XCXFiles/xsp/img/pic/pic_14.jpg" },
        //     { "sCode": "300104", "sDtSecCode": "0001300104", "sName": "认养一头牛 原味酸奶 200gx12盒x2提装  ", "iUpdateTime": 1483691763, "fNow": "--", "sRate": "--", "className": "block_wave block_stop", "srcimg": "https://www.e-taiyun.cn/XCXFiles/xsp/img/pic/pic_15.jpg" }];
        // //  optionsList = this.optionsDataTranlate(optionsList, "");
        // // 开始加载页面
        // var scrollViewHeight = systemInfo.windowHeight - 47;
        // var scrollViewWidth = systemInfo.windowWidth-20;
        // this._SetData({
        //     optionsListData: optionsList,
        //     'scrollPosition.scrollViewWidth': scrollViewWidth,
        //     'scrollPosition.scrollViewHeight': scrollViewHeight,
        //     'scrollPosition.windowViewHeight': systemInfo.windowHeight,
        //     flag: false,
        //     tabIndex: 0
        // })

    }

    /**
     * 
     * @param {*} e
     * @returns {void}
     * @memberof IndexPage
     */
    public handletouchmoveHandler(e: any): void {
        //  console.log(e)
        if (this.Data.currentGesture != 0) {
            return
        }
        let currentX = e.touches[0].pageX
        let currentY = e.touches[0].pageY
        let tx = currentX - this.Data.lastX
        let ty = currentY - this.Data.lastY
        let text = ""
        //左右方向滑动
        if (Math.abs(tx) > Math.abs(ty)) {
            if (tx < 0) {
                text = "向左滑动"
                this.Data.currentGesture = 1
            }
            else if (tx > 0) {
                text = "向右滑动"
                this.Data.currentGesture = 2
            }

        }
        //上下方向滑动
        else {
            if (ty < 0) {
                text = "向上滑动"
                this.Data.currentGesture = 3

            }
            else if (ty > 0) {
                text = "向下滑动"
                this.Data.currentGesture = 4
            }

        }

        //将当前坐标进行保存以进行下一次计算
        this.Data.lastX = currentX
        this.Data.lastY = currentY
        this.Data.ballTop = e.touches[0].pageY;
        this.Data.ballLeft = e.touches[0].pageX;
        this.Data.ApplyChange(this);
    }

    /**
     * 
     * @param {*} e
     * @returns {void}
     * @memberof IndexPage
     */
    public handletouchtartHandler(e: any): void {
        console.log(e);
        this.Data.lastX = e.touches[0].pageX;
        this.Data.lastY = e.touches[0].pageY;
    }

    /**
     * 
     * @param {*} e
     * @returns {void}
     * @memberof IndexPage
     */
    public handletouchend(e: any): void {
        console.log(e);
        this.Data.currentGesture = 0
        this.Data.text = "没有滑动";
        this.Data.ApplyChange(this);
    }

    //     bindscroll(e) {
    //         var scrollTop = e.detail.scrollTop;
    //         var scroll;
    //         this._SetData({
    //             'scrollPosition.scrollTop': scrollTop
    //         })
    //     }
    //     getOptionInfo(code) {
    //         for (var i = 0, j = this.data.optionsListData.length; i < j; i++) {
    //             var optionData = this.data.optionsListData[i];
    //             if (optionData.sDtSecCode == code) {
    //                 optionData.selectIndex = i;
    //                 return optionData;
    //             }
    //         }
    //         return {};
    //     }
    //     getPositionDomByXY(potions) {
    //         var y = potions.y - this.data.scrollPosition.top + this.data.scrollPosition.scrollTop;
    //         var optionsListData = this.data.optionsListData;
    //         var everyOptionCell = this.data.scrollPosition.everyOptionCell;
    //         for (var i = 0, j = optionsListData.length; i < j; i++) {
    //             if (y >= i * everyOptionCell && y < (i + 1) * everyOptionCell) {
    //                 return optionsListData[i];
    //             }
    //         }
    //         return optionsListData[0];
    //     }
    //     draggleTouch(e) {
    //         var touchType = e.type;
    //         switch (touchType) {
    //             case "touchstart":
    //                 this.scrollTouchStart(e);
    //                 break;
    //             case "touchmove":
    //                 this.scrollTouchMove(e);
    //                 break;
    //             case "touchend":
    //                 this.scrollTouchEnd(e);
    //                 break;
    //         }
    //     }
    //     scrollTouchStart(e) {
    //         // console.log(e);
    //         var firstTouchPosition = {
    //             x: e.changedTouches[0].pageX,
    //             y: e.changedTouches[0].pageY,
    //         }
    //         var domData = this.getPositionDomByXY(firstTouchPosition);

    //         //movable-area滑块位置处理
    //         var movableX = firstTouchPosition.x;
    //         var movableY = firstTouchPosition.y - this.data.scrollPosition.top - this.data.scrollPosition.everyOptionCell / 2;

    //         console.log("firstTouchPosition:", firstTouchPosition);
    //         console.log("domData:", domData);
    //         console.log("movableX", movableX);
    //         console.log("movableY", movableY)
    //         this._SetData({
    //             movableViewPosition: {
    //                 x: movableX,
    //                 y: movableY,
    //                 className: "",
    //                 data: domData
    //             }
    //         })

    //         var secCode = domData.sDtSecCode;
    //         var secInfo = this.getOptionInfo(secCode);
    //         secInfo.selectPosition = e.changedTouches[0].clientY;
    //         secInfo.selectClass = "dragSelected";
    //         this.data.optionsListData[secInfo.selectIndex].selectClass = "dragSelected";
    //         var optionsListData = this.data.optionsListData;
    //         console.log("secInfo", secInfo);
    //         console.log("optionsListData", optionsListData);
    //         console.log("secInfo.selectIndex", secInfo.selectIndex);
    //         this._SetData({
    //             'scrollPosition.scrollY': false,
    //             selectItemInfo: secInfo,
    //             optionsListData: optionsListData,
    //             'scrollPosition.selectIndex': secInfo.selectIndex
    //         })
    //     }
    //     scrollTouchMove(e) {
    //         var selectItemInfo = this.data.selectItemInfo;
    //         var selectPosition = selectItemInfo.selectPosition;
    //         var moveDistance = e.changedTouches[0].clientY;
    //         var everyOptionCell = this.data.scrollPosition.everyOptionCell;
    //         var optionsListData = this.data.optionsListData;
    //         var selectIndex = selectItemInfo.selectIndex;

    //         console.log("e.changedTouches:", e.changedTouches);
    //         //movable-area滑块位置处理
    //         var movableX = e.changedTouches[0].pageX;
    //         var movableY = e.changedTouches[0].pageY - this.data.scrollPosition.top - this.data.scrollPosition.everyOptionCell;


    //         this._SetData({
    //             movableViewPosition: {
    //                 x: movableX,
    //                 y: movableY,
    //                 className: "",
    //                 data: this.data.movableViewPosition.data
    //             }
    //         })

    //         if (moveDistance - selectPosition > 0 && selectIndex < optionsListData.length - 1) {
    //             if (optionsListData[selectIndex].sDtSecCode == selectItemInfo.sDtSecCode) {
    //                 optionsListData.splice(selectIndex, 1);
    //                 optionsListData.splice(++selectIndex, 0, selectItemInfo);
    //                 selectPosition += everyOptionCell;
    //             }
    //         }

    //         if (moveDistance - selectPosition < 0 && selectIndex > 0) {
    //             if (optionsListData[selectIndex].sDtSecCode == selectItemInfo.sDtSecCode) {
    //                 optionsListData.splice(selectIndex, 1);
    //                 optionsListData.splice(--selectIndex, 0, selectItemInfo);
    //                 selectPosition -= everyOptionCell;
    //             }
    //         }

    //         this._SetData({
    //             'selectItemInfo.selectPosition': selectPosition,
    //             'selectItemInfo.selectIndex': selectIndex,
    //             optionsListData: optionsListData,
    //         });
    //     }
    //     scrollTouchEnd(e) {
    //         console.log(e);
    //         var optionsListData = this.optionsDataTranlate(this.data.optionsListData, "");

    //         this._SetData({
    //             optionsListData: optionsListData,
    //             'scrollPosition.scrollY': true,
    //             'movableViewPosition.className': "none"
    //         })
    //     }
    //     optionsDataTranlate(optionsList, selectClass) {
    //         for (var i = 0, j = optionsList.length; i < j; i++) {
    //             optionsList[i].selectClass = selectClass;
    //         }
    //         return optionsList;
    //     }

}

Page(new IndexPage());
